package health

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/health"
	healthReq "gitcode.com/G-YT/iHealth25/server/model/health/request"
	"gitcode.com/G-YT/iHealth25/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ShipuApi struct {
}

var shipuService = service.ServiceGroupApp.HealthServiceGroup.ShipuService

// CreateShipu 创建Shipu
// @Tags Shipu
// @Summary 创建Shipu
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.Shipu true "创建Shipu"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /shipu/createShipu [post]
func (shipuApi *ShipuApi) CreateShipu(c *gin.Context) {
	var shipu health.Shipu
	err := c.ShouldBindJSON(&shipu)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := shipuService.CreateShipu(&shipu); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteShipu 删除Shipu
// @Tags Shipu
// @Summary 删除Shipu
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.Shipu true "删除Shipu"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /shipu/deleteShipu [delete]
func (shipuApi *ShipuApi) DeleteShipu(c *gin.Context) {
	var shipu health.Shipu
	err := c.ShouldBindJSON(&shipu)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := shipuService.DeleteShipu(shipu); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteShipuByIds 批量删除Shipu
// @Tags Shipu
// @Summary 批量删除Shipu
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Shipu"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /shipu/deleteShipuByIds [delete]
func (shipuApi *ShipuApi) DeleteShipuByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := shipuService.DeleteShipuByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateShipu 更新Shipu
// @Tags Shipu
// @Summary 更新Shipu
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.Shipu true "更新Shipu"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /shipu/updateShipu [put]
func (shipuApi *ShipuApi) UpdateShipu(c *gin.Context) {
	var shipu health.Shipu
	err := c.ShouldBindJSON(&shipu)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := shipuService.UpdateShipu(shipu); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindShipu 用id查询Shipu
// @Tags Shipu
// @Summary 用id查询Shipu
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query health.Shipu true "用id查询Shipu"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /shipu/findShipu [get]
func (shipuApi *ShipuApi) FindShipu(c *gin.Context) {
	var shipu health.Shipu
	err := c.ShouldBindQuery(&shipu)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reshipu, err := shipuService.GetShipu(shipu.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reshipu": reshipu}, c)
	}
}

// GetShipuList 分页获取Shipu列表
// @Tags Shipu
// @Summary 分页获取Shipu列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query healthReq.ShipuSearch true "分页获取Shipu列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /shipu/getShipuList [get]
func (shipuApi *ShipuApi) GetShipuList(c *gin.Context) {
	var pageInfo healthReq.ShipuSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := shipuService.GetShipuInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
